class Solution {
    public static String longestPalindrome(String s) {
        int begin = 0, end = 0;
        int maxLen = -1;
        for (int i = 0; i < s.length(); i++) {
            int left = i, right = i;
            while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            if (right - left - 1 > maxLen) {
                begin = left + 1;
                end = right - 1;
                maxLen = right - left - 1;
            }

            left = i;
            right = i + 1;
            while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }

            if (right - left - 1 > maxLen) {
                begin = left + 1;
                end = right - 1;
                maxLen = right - left - 1;
            }

        }
        return s.substring(begin, end + 1);
    }

    public static void main(String[] args) {
        System.out.println(longestPalindrome("babad"));
    }
}